<?php

namespace App\Models\OperationManagement;

use Grimzy\LaravelMysqlSpatial\Types\Point;
use Grimzy\LaravelMysqlSpatial\Types\Polygon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;

class JobScope extends Model
{
    use SoftDeletes;
    use SpatialTrait;
    protected $guarded = [];
    protected $spatialFields = ['areas'];

    //查询list
    public function getList($request)
    {
    	$per_page = $request->input('per_page',10);
    	return self::orderBy('id','desc')
                ->with('area.city')
                ->where(function($query) use ($request){
                    //检测当前的请求中是否包含road_name、road_level参数
                    $road_name = $request->input('road_name', '');//道路名称
                    $road_level = $request->input('road_level','');//道路等级
                    $areas = $request->input('areas','');
                    if(!empty($road_name)) {
                        $query->where('road_name','like','%'.$road_name.'%');
                    }
                    if(!empty($road_level)) {
                        $query->where('road_level','=',$road_level);
                    }
                    if(!empty($location)) {
                        $areas = Polygon::fromString(areas);
                        $query->distance('areas', $areas, 10);
                    }
                })
                ->paginate($per_page)->toArray();
    }

    //查询顶级路段数据
    public function getParentList($request)
    {
        $per_page = $request->input('per_page',10);
        return self::orderBy('id','desc')
                ->with(['areas'=>function($query){
                    $query->with('city');
                }])
                ->where(function($query) use ($request){
                    //检测当前的请求中是否包含number、road_name、type参数
                    $number = $request->input('number','');//道路编号
                    $road_name = $request->input('road_name', '');//道路名称
                    $type = $request->input('type','');//幅路类型
                    if(!empty($number)) {
                        $query->where('number','=',$number);
                    }
                    if(!empty($road_name)) {
                        $query->where('road_name','like','%'.$road_name.'%');
                    }
                    if(!empty($type)) {
                        $query->where('type','=',$type);
                    }
                    $query->whereNull('parent_id');
                })
                ->paginate($per_page)->toArray();
    }

    //查询分段路段数据
    public function getSonList($request)
    {
        $per_page = $request->input('per_page',10);
        return self::orderBy('id','desc')
                ->with(['areas'=>function($query){
                    $query->with('city');
                }])
                ->where(function($query) use ($request){
                    //检测当前的请求中是否包含number、road_name、type参数
                    $number = $request->input('number','');//道路编号
                    $road_name = $request->input('road_name', '');//道路名称
                    $type = $request->input('type','');//幅路类型
                    $parent_id = $request->input('parent_id','');//父级id
                    if(!empty($number)) {
                        $query->where('number','=',$number);
                    }
                    if(!empty($road_name)) {
                        $query->where('road_name','like','%'.$road_name.'%');
                    }
                    if(!empty($type)) {
                        $query->where('type','=',$type);
                    }
                    $query->where('parent_id',$parent_id);
                })
                ->paginate($per_page)->toArray();
    }

    //查询城市、区域
    public function area()
    {
        return $this->belongsTo(\App\Models\CarModels\CarArea::class,'area_id','id')->select('id','name','city_id');
    }
}
